import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../providers/selected_item_provider.dart';
import '../../providers/favorites_provider.dart';

class DetailPage extends ConsumerWidget {
  const DetailPage({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    // 再次使用 ref.watch 来获取当前选中的产品
    final selectedItem = ref.watch(selectedItemProvider);

    if (selectedItem == null) {
      return Scaffold(
        appBar: AppBar(title: const Text('错误')),
        body: const Center(child: Text('没有选择任何项目。')),
      );
    }

    // 同样监听收藏状态，与 HomePage 的收藏按钮状态保持同步
    final isFavorite = ref.watch(favoritesProvider.select((favs) => favs.contains(selectedItem.id)));

    return Scaffold(
      appBar: AppBar(
        title: Text(selectedItem.productName ?? '详情'),
        actions: [
          IconButton(
            icon: Icon(
              isFavorite ? Icons.favorite : Icons.favorite_border,
              color: isFavorite ? Colors.red : Colors.white,
            ),
            onPressed: () {
              ref.read(favoritesProvider.notifier).toggleFavorite(selectedItem.id);
            },
          ),
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('产品ID: ${selectedItem.pid ?? 'N/A'}', style: const TextStyle(fontSize: 18)),
            // ... 其他信息
          ],
        ),
      ),
    );
  }
}
